package cn.com.dhcc.app.pub.module.codematic.service;


import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

import org.springframework.util.ResourceUtils;

import cn.com.dhcc.app.core.util.FreemarkerUtil;
import cn.com.dhcc.app.pub.module.codematic.vo.Bean;

public class ControllerGenerator {
	public static void generate(String appRoot,Class<?> clazz, Bean entity) throws Exception {
		File template = ResourceUtils.getFile("classpath:template/codematic/controller_template.ftl");
		File pkg =  ResourceUtils.getFile(appRoot+ "/codematic/main/java/"+ (entity.getAppPkg() + ".web.controller." + entity.getControllerPkg()).replaceAll("\\.", "/"));
		if(!pkg.exists()){
			pkg.mkdirs();
		}
		Writer fileWriter = new FileWriter(new File(pkg.getAbsoluteFile() + "/" + entity.getBeanName()+ "Controller.java"));
		FreemarkerUtil.flushData(template.getAbsolutePath(), fileWriter, entity);
	}
}
